package task.snake;

import javax.swing.JOptionPane;

import task.snake.controller.StartListener;
import task.snake.controller.StopListener;
import task.snake.controller.TurnListener;
import task.snake.model.SnakeModel;
import task.snake.view.swingimpl.SnakeWindow;

/**
 * 
 * @author Nadejda Makarkina
 * 
 */
public class StartPoint {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int m = 0; 				// width of field
		int n = 0; 				// height of field
		int length = 0; 		// length of snake
		int quantity = 0; 		// quantity of frogs
		long sleepTime = 0; 	// sleep time of frog in milliseconds
		SnakeModel snakeModel;
		SnakeWindow window;

		if (args.length == 5) {
			try {
				m = Integer.parseInt(args[0]);
				n = Integer.parseInt(args[1]);
				length = Integer.parseInt(args[2]);
				quantity = Integer.parseInt(args[3]);
				sleepTime = Long.parseLong(args[4]);
			} catch (NumberFormatException e) {
				System.err.println("Argument" + " must be a number");
				System.exit(1);
			}
		} else {
			m = 10;
			n = 10;
			length = 2;
			quantity = 5;
			sleepTime = 800;
			JOptionPane.showMessageDialog(null, "Invalid number of arguments. \n Default values were used.");
		}
		snakeModel = new SnakeModel(m, n, length, quantity, sleepTime);
		window = new SnakeWindow(snakeModel.getWidth(), snakeModel.getHeight());
		window.setStartListener(new StartListener(snakeModel));
		window.setStopListener(new StopListener(snakeModel));
		window.setTurnListener(new TurnListener(snakeModel));
		snakeModel.addObserver(window);
		window.update(snakeModel, null);		
	}

}
